package com.dnxh.controller;

import cn.hutool.core.lang.Pair;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.digest.MD5;
import com.dnxh.constant.CommonConstant;
import com.dnxh.constant.CustomResult;
import com.dnxh.entity.request.CreateAccountRequest;
import com.dnxh.entity.request.ModifyPassRequest;
import com.dnxh.entity.request.SearchAdminRequest;
import com.dnxh.service.AccountService;
import com.dnxh.util.JwtUtil;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authz.AuthorizationException;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.apache.shiro.subject.Subject;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author zhiqiu
 * @since 2022 -04-06
 */
@RestController
@RequestMapping("/account")
public class AccountController {

	@Resource
	private AccountService accountService;
	
	/**
	 * 登录
	 *
	 * @param user     the user
	 * @param password the password
	 * @return the custom result
	 */
	@GetMapping("/login")
	public CustomResult login(@RequestParam("user") String user,
	                          @RequestParam("password") String password){
		if (StrUtil.isEmpty (user) || StrUtil.isEmpty (password)){
			return CustomResult.fail ("用户名或密码为空!");
		}
		if (!accountService.verify (user,password)){
			return CustomResult.fail ("密码不匹配!");
		}
		return CustomResult.success (JwtUtil.encode (MapUtil.of (new Pair<> (CommonConstant.JWT_USER_NAME,user))));
	}
	
	/**
	 * 查询管理员信息
	 *
	 * @param request the request
	 * @return the custom result
	 */
	@PostMapping("/search")
	public CustomResult search(@RequestBody@Valid SearchAdminRequest request){
		return accountService.search(request);
	}
	
	/**
	 * 创建账户
	 *
	 * @param request the request
	 * @return the string
	 */
	@RequiresRoles (logical = Logical.OR,value = "ROOT")
	@PostMapping("/add")
	public CustomResult addAccount(@RequestBody CreateAccountRequest request){
		return accountService.addAccount(request);
	}
	
	/**
	 * 删除账户
	 *
	 * @param id the id
	 * @return the custom result
	 */
	@RequiresRoles (logical = Logical.OR,value = "ROOT")
	@DeleteMapping("/{id}")
	public CustomResult delete(@PathVariable("id") Long id){
		return accountService.deleteAccount(id);
	}

	// TODO: 2022/4/11 ID，名称，地点，时间，类型(比赛，会议)   ==> 模板    t_account_会议(id,account_id,会议_id,地点)
	// TODO: 2022/4/11 1. 发布会议/比赛
	//                 2. 添加参加人员
	//                 3. 发布通知(手动)
	
	/**
	 * 修改登录密码
	 *
	 * @param request the request
	 * @return the custom result
	 */
	@RequiresRoles (logical = Logical.OR,value = "ADMIN")
	@PostMapping("/update")
	public CustomResult modifyPassword(@RequestBody@Valid ModifyPassRequest request){
		return accountService.modifyPassword(request);
	}
	
	/**
	 * 添加一个部门
	 *
	 * @param department the department
	 * @return the custom result
	 */
	@PutMapping("/create/department")
	public CustomResult addDepartment(@RequestParam("department") String department){
		return accountService.addDepartment(department);
	}
	
	/**
	 * 删除部门
	 *
	 * @param id the id
	 * @return the custom result
	 */
	@DeleteMapping("/delete/department/{id}")
	public CustomResult deleteDepartment(@PathVariable("id") Long id){
		return accountService.deleteDepartment(id);
	}
}